package org.alma.opengl

import javax.media.opengl._
import javax.media.opengl.glu.GLU
import java.io.File


class Renderer extends GLEventListener 
{
  var tex: Texture = null 
 
  protected def glu = new GLU
  
  def display(drawable: GLAutoDrawable)
  {
    val gl = drawable.getGL
    
    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
    gl.glLoadIdentity
    gl.glColor3f(1.0f, 0.0f, 0.0f)
    
    beginBlock(gl, GL.GL_TRIANGLES,
    {
      gl.glVertex3f(0.0f, 1.0f, 0.0f)
      gl.glVertex3f(-1.0f, -1.0f, 0.0f)
      gl.glVertex3f(1.0f, -1.0f, 0.0f)
    })
    
    gl.glFlush
    
  }
  
  def init(drawable: GLAutoDrawable)
  {
    val gl = drawable.getGL
      
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f)  
    gl.glPointSize(4.0f)
    
  }
  
  def displayChanged(drawable: GLAutoDrawable, modeChanged: Boolean, deviceChanged: Boolean)
  {
    /* ignore, not implemented in JOGL */
  }
  
  def reshape(drawable: GLAutoDrawable, x: Int, y: Int, width: Int, height: Int)
  {
    /*
    val gl = drawable.getGL
    val newHeight = if (height <= 0) 1 else height
    
    val h: Float = width.toFloat / newHeight.toFloat
    
    gl.glViewport(0, 0, width, newHeight)
    gl.glMatrixMode(GL.GL_PROJECTION)
    gl.glLoadIdentity()
    glu.gluPerspective(45.0f, h, 1.0, 20.0)
    gl.glMatrixMode(GL.GL_MODELVIEW)
    gl.glLoadIdentity()
    */
  }
  
  protected def beginBlock(gl: GL, target: Int, block: => Unit)
  {
    gl.glBegin(target)
    block
    gl.glEnd
  }
  
}
